package com.woniu.user.controller;

import com.woniu.common.entity.*;
import com.woniu.user.feign.ImmuneFeign;
import com.woniu.user.feign.PunishComplainNoticeFeign;
import com.woniu.user.service.PetService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author author
 * @create 2021/5/19
 */
@RequestMapping("/pet")
@RestController
public class PetController {

    @Autowired
    PetService petService;

    @Autowired
    PunishComplainNoticeFeign punishComplainNoticeFeign;

    @Autowired
    ImmuneFeign immuneFeign;

    //远程调用全部惩罚
    @RequestMapping("/findAllPunishment")
    public PageResult<PunishmentRecord> findAllPunishment(@RequestParam("page") Integer page, @RequestParam("size") Integer size){

        return punishComplainNoticeFeign.findAllPunishment(page, size);
    }

    //远程调用年审记录
    @GetMapping("/getdogbyuid")
    public ResponseResult<List<OrderInfo>> getdogbyuid(Integer uid) {
        return immuneFeign.getdogbyuid(uid);
    }

    //远程调用处罚记录
    @RequestMapping("/punishment")
    public PageResult<PunishmentRecord> findPunishmentByUid(Integer u_id, Integer page, Integer size){
        return punishComplainNoticeFeign.findPunishmentByUid(u_id, page, size);
    }

    //查询电子犬证
    @RequestMapping("/selectdogetc_image")
    public  ResponseResult<List<Dog>> selectdogetc_image(Integer u_id){
        if(petService.selectdogetc_image(u_id).equals("")){
            return new ResponseResult<List<Dog>>(500, null, "请先上传电子犬证", petService.selectdogetc_image(u_id), null);
        }
        return new ResponseResult<List<Dog>>(200, null, "查询成功", petService.selectdogetc_image(u_id), null);
    }

    //上传电子犬证
    @PostMapping("/addetcImage")
    public ResponseResult<String> addetcImage(@RequestBody Dog dog){

        System.out.println(dog);

        if (petService.addetcImage(dog)){

            return new ResponseResult<>(200, null, "保存成功", null, null);

        }
        return new ResponseResult<>(500, null, "保存失败", null, null);
    }

    @RequestMapping("/petInfo")
    public ResponseResult<List<Dog>> selectDogByUserId( Integer u_id) {

        List<Dog> dogs = petService.selectDogByUserid(u_id);

        return new ResponseResult<List<Dog>>(200, null, "查询成功", dogs, null);
    }
    @ResponseBody
    @RequestMapping("/selectDog")
    public ResponseResult<Dog> selectDog(@Param("name")String name,@Param("icd")String icd
    ,@Param("tel")String tel,@Param("dogetc")String dogetc){
        ResponseResult<Dog> result = new ResponseResult<>();
        if(name == null && name.equals("")){
            result.setCode(404);
            result.setMessage("请输入犬主姓名");
            return result;
        }
        if(icd == null && icd.equals("")){
            result.setCode(404);
            result.setMessage("请输入犬主证件号");
            return result;
        }
        if(tel == null && "".equals(tel)){
            result.setCode(404);
            result.setMessage("请输入犬主电话");
            return result;
        }
        if(dogetc == null && "".equals(dogetc)){
            result.setCode(404);
            result.setMessage("请输入犬牌编号");
            return result;
        }
        Dog dog = petService.selectDog(name, icd, tel, dogetc);
        if(dog == null){
            result.setCode(202);
            result.setMessage("查无此狗");
            return result;
        }
        result.setCode(200);
        result.setMessage("查询成功");
        result.setData(dog);
        return result;
    }
}
